# -*- coding:utf-8 -*-
# @Author:🎈RedBalloon
# @Time:2022/10/2-15:05
# @File:15-requests库的使用.py
import requests
from fake_useragent import UserAgent
# 1.requests库的安装
# pip install requests
# pip install -i https://pypi.tuna.tsinghua.edu.cn/simple some-package


# 2.请求
# requests.get(url)
# requests.post('http://httpbin.org/post')
# requests.put('http://httpbin.org/put')
# requests.head('http://httpbin.org/head')
# requests.delete('http://httpbin.org/delete')
# requests.options('http://httpbin.org/options')


# 2.1 GET请求
# 参数是字典,也可以传递json类型. 可以直接复制请求链接或使用params
# https://www.baidu.com/s?wd=英雄联盟
# https://www.baidu.com/s?wd=%E8%8B%B1%E9%9B%84%E8%81%94%E7%9B%9F
# url = "https://baidu.com/s?"
# params = {
#     "wd": "英雄联盟",
# }
# # params直接传字典,而且不需要转码
# response = requests.get(url, params=params)
# print(response)
# # 会自动拼接上去
# print(response.url)  # http://www.baidu.com/s?wd=%E8%8B%B1%E9%9B%84%E8%81%94%E7%9B%9F


# 2.2 POST请求
# 参数是字典,也可以传递json类型
# url = "https://www.qbiqu.com/login.php?do=submit&jumpurl=http%3A%2F%2Fwww.qbiqu.com%2Fuserdetail.php%3Fuid%3Dundefined"
# headers = {
#     "User-Agent": UserAgent().random,
# }
# form_data = {
#     "username": "redballoon",
#     "password": "123456",
#     "usecookie": 0,
#     "action": "login"
# }
# response = requests.post(url, headers=headers, data=form_data)
# response.encoding = 'gbk'
# html = response.text
# print(html)


# 2.3 设置请求头,timeout
# url = 'https://www.baidu.com/'
# headers = {
#     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36 Edg/105.0.1343.53"
# }
# response = requests.get(url, headers=headers, timeout=3)
# print(response)


# 2.4 设置代理
# url = 'http://httpbin.org/get'
# proxies = {
#     'http': 'http://183.233.169.226:9091',
#     # 'https': ''
# }
# 需要账户密码的
# proxies2 = {
#     "http": "http://user:pass@10.10.1.10:3124/",
#     "http": "http://账号:密码@ip:端口/",
# }
# response = requests.get(url, proxies=proxies, timeout=3)
# print(response.text)


# 2.5 设置session保存cookies 会话保持
# 创建一个session对象
# session = requests.Session()
# # 用session对象发出get请求,设置cookies
# response = session.get("https://httpbin.org/cookies/set/sessioncookie/qwer123")
# print(response.text)


# 2.6 ssl的验证
# 禁用安全请求的警告
# requests.packages.urllib3.disable_warnings()

# 在requests中只需要添加一个参数 verify=False 即可忽略证书的警告
# verify默认是True
# response = requests.get("https://www.12306.cn/index/", verify=False, timeout=3)


# 3 获取响应信息
# response.json()
# response.text
# response.url
# response.content
# response.headers
# response.encoding
# response.requests.headers
# response.cookie

